package com.avic.rong.course.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.activity.login.LoginPhoneActivity;
import com.avic.rong.course.activity.CourseDetailActivity;
import com.avic.rong.course.activity.otherActivity.VideoPlayerActivity;
import com.avic.rong.course.bean.PeiXunClassMuLuBean;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.ToastTools;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Chenjiaxin on 2019/8/18 10:31
 * E-Mail Address：16601203965@163.com
 */
public class CourseMuLuAdapter extends RecyclerView.Adapter<CourseMuLuAdapter.CourseMuLuViewHolder> {

    private Context context;
    private List<PeiXunClassMuLuBean.DatasBean> datasBeanList = new ArrayList<>();
    private LayoutInflater inflater;
    private String firstImageUrl = ConstantsUtils.EMPTY_STR;


    public void setFirstImageUrl(String firstImageUrl) {
        this.firstImageUrl = firstImageUrl;
    }

    public CourseMuLuAdapter(Context context, List<PeiXunClassMuLuBean.DatasBean>  datasBeanList) {
        this.context = context;
        this.datasBeanList = datasBeanList;
        this.inflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public CourseMuLuViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.view_course_mulu_item, viewGroup, false);
        return new CourseMuLuViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CourseMuLuViewHolder holder, int position) {

        PeiXunClassMuLuBean.DatasBean datasBean = datasBeanList.get(position);
        holder.tv_course_name.setText(datasBean.getName());
        CourseMuLuSecondAdapter courseMuLuSecondAdapter = new CourseMuLuSecondAdapter(context,datasBean.getHourList(),firstImageUrl);
        DividerItemDecoration divider = new DividerItemDecoration(context, DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(context, R.drawable.divider_05_pix));
        holder.recycler_hourlist.addItemDecoration(divider);
        holder.recycler_hourlist.setLayoutManager(new LinearLayoutManager(context));
        holder.recycler_hourlist.setAdapter(courseMuLuSecondAdapter);

    }

    @Override
    public int getItemCount() {
        return datasBeanList.size();
    }


    class MuLuListener implements View.OnClickListener{
        PeiXunClassMuLuBean.DatasBean.HourListBean hourListBean;

        public MuLuListener(PeiXunClassMuLuBean.DatasBean.HourListBean hourListBean) {
            this.hourListBean = hourListBean;
        }

        @Override
        public void onClick(View v) {
            if (SharedPreferencesUtil.isLogin()) {
                context.startActivity(VideoPlayerActivity.getIntent(context,hourListBean.getName(),hourListBean.getContent(),firstImageUrl));
            }else{
                ToastTools.showToast("登录后才可观看");
                context.startActivity(new Intent(context,LoginPhoneActivity.class));
            }

        }
    }

    class CourseMuLuViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_course_name)
        public TextView tv_course_name;
        @BindView(R.id.recycler_hourlist)
        public RecyclerView recycler_hourlist;


        public CourseMuLuViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
